<?php

declare(strict_types=1);

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Slim\App;

class ExampleMiddlewareV2_2 implements MiddlewareInterface
{
    private ResponseFactoryInterface $responseFactory;

    private App $app;


    public function __construct(ResponseFactoryInterface $responseFactory, App $app)
    {
        $this->responseFactory = $responseFactory;
        $this->app = $app;
    }
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // Check some condition to determine if a new response should be created
        $flag = true;
        if ($flag) {
            // Create a new response using the response factory
            $response = $this->app->getResponseFactory()->createResponse();
            // $response = $this->responseFactory->createResponse();
            $response->getBody()->write('New response created by middleware');
            return $response;
        }
        $response = $handler->handle($request);
        return $response;
    }
}